/*
 * Copyright 2007 Jeffrey Palm
 */

import java.io.*;
import java.util.*;

/**
 * Creates a Python world
 */
class PythonSink extends AbstractOOSink {

  PythonSink(File outdir) {super(outdir,"\t");}

  // --------------------------------------------------
  // AbstractOOSink
  // --------------------------------------------------

  String typeHeader(World.Type t) {return "import 'support'";}

  String typeFooter(World.Type t) {return null;}

  boolean wantsPublic() {return false;}

  String getExtends() {return "<";}

  boolean wantMembers() {return false;}

  String formatMember(World.Decl d) {return null;}

  String constructorStart(String name) {return "def __init_";}
  

  String methodStart(World.Decl d) {return "def";}

  boolean useParamType() {return false;}

  String thiz(String name) {return "self." + name;}
  String methodThiz(String name) {return name;}

  String var(String name) {return name;}

  String lineComment() {return "#";}

  String comment(String s) {return "/* " + s + " */";}

  String openBrace() {return ":\n";}
  String closeBrace() {return "\n";}

  void createMethodBody(PrintStream out, World.Type t, World.Decl d) {
    Support.Param[] ps = new Support.Param[d.getParams().size()];
    int i = 0;
    for (World.Param p : d.getParams()) {
      String name = p.getName();
      String value;
      if (p.getType() != null) {
        value = p.getName();
      } else {
        value = getInvokeMethod(t,d.getParams(),p);
      }
      ps[i++] = Support.newParam(name,"' + urlencode(" + value + ") + '"); // unquote it
    }
    String page = Support.getPage(t.getName(),d.getName(),ps, new boolean[]{true,false});
    out.print(tab + tab + "return extractResult('" + Support.getHost() + "','" + page + "');");

  }

  // --------------------------------------------------
  // FileSink
  // --------------------------------------------------

  String getFileName(World.Type t) {
    return t.getName() + ".class.py";
  }

  String name() {
    return "py";
  }

  /** Write out include.py */
  void postpareRest() {
    try {
      PrintStream out = new PrintStream(new FileOutputStream(new File(getOutdir(),"include.py")));
      for (String n : getFileNames()) {
        out.println("require '" + n + "'");
      }
      out.close();
    } catch (Exception e) {e.printStackTrace();}
  }

}